# --------------------------
# 跨平台静态编译配置
# 目标：消除第三方 DLL 依赖
# 最低兼容性：Windows 10 或 Windows 7 (需 KB2999226)
# --------------------------

# 编译器定义
CC = x86_64-w64-mingw32-gcc

# 编译选项
CFLAGS = -I/mingw64/include/SDL2 \
          -O2 \
          -mwindows \
          -Wall \
          -Werror=format-security \
          -fexec-charset=UTF-8 \
          -finput-charset=UTF-8

# 链接选项
LDFLAGS = -L/mingw64/lib \
           -static \
           -static-libgcc \
           -static-libstdc++

# 静态链接库分组 (解决循环依赖)
STATIC_LIBS = -Wl,--start-group \
              -Wl,-Bstatic \
              -lmingw32 \
              -lSDL2main \
              -lSDL2 \
              -lSDL2_image \
              -lSDL2_ttf \
              -limm32 \
              -lole32 \
              -loleaut32 \
              -lsetupapi \
              -lversion \
              -lshlwapi \
              -lwinmm \
              -lwinpthread \
              -lpthread \
              -Wl,--whole-archive -lstdc++ -Wl,--no-whole-archive \
              -Wl,--end-group

# 动态链接库 (仅限系统库)
DYNAMIC_LIBS = -Wl,-Bdynamic \
               -lws2_32 \
               -luuid \
               -lcomctl32 \
               -lhid \
               -lcfgmgr32

# 文件定义
SRC = main.c
TARGET = jian.exe
OBJ = $(SRC:.c=.o)

# --------------------------
# 构建规则
# --------------------------

.PHONY: all clean release debug

all: $(TARGET)

$(TARGET): $(OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# 调试版本 (保留符号)
debug: CFLAGS += -g
debug: clean all

# 发布版本 (去除符号)
release: CFLAGS += -s
release: clean all

# 清理构建产物
clean:
	rm -f $(TARGET) $(OBJ)
